package com.ruoyi.system.service.impl.fxImpl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fx.FxOrder;
import com.ruoyi.system.domain.fx.FxUser;
import com.ruoyi.system.domain.fx.FxWithdrawalRecord;
import com.ruoyi.system.domain.vo.FxMoneyVo;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.mapper.fx.FxStoreMapper;
import com.ruoyi.system.mapper.fx.FxUserMapper;
import com.ruoyi.system.mapper.fx.FxWithdrawalAccountMapper;
import com.ruoyi.system.mapper.fx.FxWithdrawalRecordMapper;
import com.ruoyi.system.service.fx.IFxOrderService;
import com.ruoyi.system.service.fx.IFxUserService;
import com.ruoyi.system.service.fx.IFxWithdrawalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 提现记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class FxWithdrawalRecordServiceImpl implements IFxWithdrawalRecordService {

    @Autowired
    private FxWithdrawalAccountMapper fxWithdrawalAccountMapper;
    @Autowired
    private FxWithdrawalRecordMapper fxWithdrawalRecordMapper;
    @Autowired
    private IFxOrderService fxOrderService;
    @Autowired
    private FxUserMapper fxUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private IFxUserService iFxUserService;

    @Autowired
    private FxStoreMapper fxStoreMapper;

    /**
     * 查询提现记录
     *
     * @param id 提现记录主键
     * @return 提现记录
     */
    @Override
    public FxWithdrawalRecord selectFxWithdrawalRecordById(Integer id) {
        return fxWithdrawalRecordMapper.selectFxWithdrawalRecordById(id);
    }

    /**
     * 查询提现记录列表
     *
     * @param fxWithdrawalRecord 提现记录
     * @return 提现记录
     */
    @Override
    public List<FxWithdrawalRecord> selectFxWithdrawalRecordList(FxWithdrawalRecord fxWithdrawalRecord) {
        Long userId = SecurityUtils.getUserId();

        // 登陆人角色列表
        List<Long> roles = SecurityUtils.getLoginUser().getUser().getRoles()
                .stream().map(SysRole::getRoleId).collect(Collectors.toList());
//        FxUser fxUser = new FxUser();
//        fxUser.setSysUserid(userId);
//        List<FxUser> userList = fxUserMapper.selectFxUserList(fxUser);
//        if (userList.size() == 0) {
//            return new ArrayList<>();
//        }
//        Integer role = sysUserRoleMapper.selectByUserId(userId);
        // 判断是否是超级管理员
//        fxWithdrawalRecord.setStatus(1);
        if (roles.contains(1L) || roles.contains(6L)) {
            return fxWithdrawalRecordMapper.selectFxWithdrawalRecordList(fxWithdrawalRecord);
        }
        FxUser fxuser = fxUserMapper.selectFxUserByUserId(userId);
        if (roles.contains(4L)) { //店铺
            fxWithdrawalRecord.setStoreId(fxuser.getStoreId());
        }

//        if (StringUtils.isNotEmpty(userList)) {
//            Long roleId = userList.get(0).getRoleType();
//            if (roleId == 3L) //为3是供应商
//                return new ArrayList<>();
//            if (roleId == 2L) // 用户
//                return new ArrayList<>();
//            if (roleId== 4) {//
//                FxStore fxStore= fxStoreMapper.selectFxStoreById(userList.get(0).getStoreId());
//                if (fxStore.getStatus() == 0) {
//                    return new ArrayList<>();
//                }
//                Long storeId = userList.get(0).getStoreId();
//                fxWithdrawalRecord.setStoreId(Convert.toInt(storeId));
//            }
//        }
        return fxWithdrawalRecordMapper.selectFxWithdrawalRecordList(fxWithdrawalRecord);
    }

    /**
     * 新增提现记录
     *
     * @param fxWithdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int insertFxWithdrawalRecord(FxWithdrawalRecord fxWithdrawalRecord) {
        FxUser fxUser = iFxUserService.selectFxUserByUserId(SecurityUtils.getUserId());
        if(fxUser.getRoleType() !=4){
            throw new GlobalException("非经销商不能提现");
        }

//        List<FxWithdrawalAccount> fxWithdrawalAccounts = fxWithdrawalAccountMapper.selectFxWithdrawalList(String.valueOf(fxWithdrawalRecord.getStoreId()));
//        if ( CollectionUtils.isEmpty(fxWithdrawalAccounts)){
//            throw new GlobalException("您还没绑定提现账号");
//        }

        if (StringUtils.isNull(fxWithdrawalRecord.getWithdrawName())) {
            throw new GlobalException("提现账号未选择");
        }

        Long storeId = Long.valueOf(fxUser.getStoreId());
        FxOrder fxorder = new FxOrder();
        fxorder.setStoreId(storeId);
        fxorder.setOrderType(2l);
        FxMoneyVo fxMoneyVo = fxOrderService.getTotalMoney(fxorder);
        BigDecimal notWithdrawn = new BigDecimal(fxMoneyVo.getNotWithdrawn());  // 未提现

        BigDecimal money = fxWithdrawalRecord.getMoney();
        BigDecimal zero = BigDecimal.ZERO;
        if (money.compareTo(zero) == 0 || money.compareTo(zero) < 0) {
            throw new GlobalException("提现金额需大于O");
        }
        if (notWithdrawn.compareTo(money) < 0) {
            throw new GlobalException("可提现余额不足");
        }
        fxWithdrawalRecord.setCreateTime(DateUtils.getNowDate());
        fxWithdrawalRecord.setStoreId(fxUser.getStoreId());
        return fxWithdrawalRecordMapper.insertFxWithdrawalRecord(fxWithdrawalRecord);
    }

    /**
     * 修改提现记录
     *
     * @param fxWithdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int updateFxWithdrawalRecord(FxWithdrawalRecord fxWithdrawalRecord) {
        fxWithdrawalRecord.setUpdateTime(new Date());
        return fxWithdrawalRecordMapper.updateFxWithdrawalRecord(fxWithdrawalRecord);
    }

    /**
     * 审核
     * @param ids
     * @return
     */
    @Override
    public int audit(Integer[] ids, Long status) {
        return fxWithdrawalRecordMapper.audit(ids, status);
    }

    /**
     * 批量修改状态
     *
     * @param ids
     * @return
     */
    @Override
    public int updateDelFlag(Integer[] ids) {
        return fxWithdrawalRecordMapper.updateStatusByIds(ids, 1);
    }

    /**
     * 批量删除提现记录
     *
     * @param ids 需要删除的提现记录主键
     * @return 结果
     */
    @Override
    public int deleteFxWithdrawalRecordByIds(Integer[] ids) {
        return fxWithdrawalRecordMapper.deleteFxWithdrawalRecordByIds(ids);
    }

    /**
     * 删除提现记录信息
     *
     * @param id 提现记录主键
     * @return 结果
     */
    @Override
    public int deleteFxWithdrawalRecordById(Integer id) {
        return fxWithdrawalRecordMapper.deleteFxWithdrawalRecordById(id);
    }
}
